<?php
namespace Admin\Controller;

class WxcontentController extends CommonController{

    public function index(){
        $this->display();
    }

    public function show(){
        $this->article = M('WxContent')->find(I('id'));
        $this->display();
    }

    public function getdata(){
        $p = I('p') ? I('p') : 1;
        $row = I('row') ? I('row') : 10;

        $m = D('WxContent');
        if($_SESSION['loginUser']) $map['uid'] = $_SESSION['loginUser']['uid'];
        $data['data'] = $m->where($map)->page($p,$row)->select();
        //$data['sql'] = $m->_sql();
		$data['page']['total'] = $m->where($map)->count();
		$data['page']['cur'] = $p;
		$data['page']['row'] = $row;
		
		_apiReturn(1, L('success'), $data, $url); //数据获取成功
    }

    public function edit(){
        if($_GET['id']){
            $this->info = M('WxContent')->find(I('id'));
        }
        $this->display();
    }

    public function edit_post(){
        if(IS_POST){
            $_POST['update_time'] = Date('Y-m-d H:i:s');
            if($_SESSION['loginUser']){
                $_POST['uid'] = $_SESSION['loginUser']['uid'];
            }
            $m = M('WxContent');
            $tmpData = $m->create($_POST);
            if($_POST['id']){
                if($m->save($tmpData)){
                    $this->success(L('success'));
                }else{
                    $this->error(L('error'));
                }
            }else{
                $tmpData['create_time'] = $tmpData['update_time'];
                if($m->add($tmpData)){
                    $this->success(L('success'));
                }else{
                    $this->error(L('error'));
                }
            }
        }
    }

    public function del(){
        $this->_del(M('WxContent'),I('ids'));
    }

    public function get_wx_content(){
        if(!I('wxurl')) _apiReturn(0,"文章链接不能为空");
        $url = I('wxurl');
        //$data = file_get_contents($html);
        //echo $data;
        
        //注意，实际应用中，content类型改成html，此处为测试方便，才选用text类型
        $reg = array(
            "title" => array("#activity-name","text"),
            "author"=>array("#post-user","text"),
            "content"=>array("#js_content","html"),
            "wdate"=>array("#post-date","text"));
        $rang = "#img-content";
        $html = file_get_contents($url);
        $html = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $html);//去除文章详细页面的干扰代码
        $arr = \QL\QueryList::Query($html,$reg,$rang)->data;
        
        if(count($arr) == 0 ){
            _apiReturn(0,"找不到匹配数据");
        }
        $article = $arr[0];
        $content = $article['content'];
        $content = str_replace('url(http:','url(/tools/img/showpic?url=http:',$content); //显示background
        $article['content'] = str_replace('data-src="','src="/tools/img/showpic?url=',$content); //显示img标签的图片
        $article['url'] = $url;
        _apiReturn(1,'success',$article);
    }
}
